''' Media Contact (MC) tests
'''
import scopt.test.common
from scopt.mpu.business.settings.handlers import media_contact


class MCCreate(scopt.test.common.ScoptTest):
    ''' MC creation tests
    MPU should:

    1. test_mc_create_unique
        allow to create MC
    2. test_mc_create_duplicate
        disallow to create MC with same name
    3. test_mc_create_read
        create MC and retrieve it with returned MC entry id
    '''
    def test_mc_create_unique(self):
        # 123-456-789 is not part of demo data
        request = self.request(media_contact.create)
        request['reqd'] = {'contact': '123-456-789'}

        response = self.send_request(request)
        response_status = response['resh']['status']

        self.assertEqual(response_status, 'OK')

    def test_mc_create_duplicate(self):
        # all_stuff@gmail.com is part of demo data
        request = self.request(media_contact.create)
        request['reqd'] = {'contact': 'all_stuff@gmail.com'}

        response = self.send_request(request)

        self.assertStatus(response, 'OK')

    def test_mc_create_read(self):
        # 123-456-789 is not part of demo data
        mc_to_create = '123-456-789'
        create_request = self.request(media_contact.create)
        create_request['reqd'] = {'contact': mc_to_create}

        create_response = self.send_request(create_request)

        select_request = self.request(media_contact.select)
        select_request['reqd'] = {'id': create_response['resd']['id']}

        select_response = self.send_request(select_request)
        response_status = select_response['resh']['status']
        select_resd = select_response['resd']
        mc_name_created = select_resd[0]['contact']

        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(select_resd), 1)
        self.assertEqual(mc_name_created, mc_to_create)


class UomSelect(scopt.test.common.ScoptTest):
    ''' MC select tests
    MPU should:

    1. test_mc_select_one
        select one MC by mc_id
    2. test_mc_select_all
        select all MCs by not providing id
    3. test_mc_select_none
        select none MC by providing id of non existent record
    '''
    def test_mc_select_one(self):
        # row with id 1 is part of demo data
        request = self.request(media_contact.select)
        request['reqd'] = {'id': 1}

        response = self.send_request(request)
        response_status = response['resh']['status']
        data = response['resd']

        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(data), 1)

    def test_mc_select_all(self):
        # there are 2 rows in mc demo data
        request = self.request(media_contact.select)
        request['reqd'] = {}

        response = self.send_request(request)
        response_status = response['resh']['status']
        data = response['resd']

        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(data), 2)

    def test_mc_select_none(self):
        # row with id of 200 is not present in demo data
        request = self.request(media_contact.select)
        request['reqd'] = {'id': 200}

        response = self.send_request(request)
        response_status = response['resh']['status']
        data = response['resd']

        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(data), 0)


class UomUpdate(scopt.test.common.ScoptTest):
    ''' MC update tests
    MPU should:

    1. test_mc_update_new_name
        allow update MC to other unique name
    2. test_mc_update_duplicate_name
        disallow update MC to name which is not unique
    3. test_mc_update_invalid_id
        disallow update MC with invalid id
    4. test_mc_update_missing_id
        disallow update MC with id is missing
    5. test_mc_update_read
        update and read MC by its ID
    '''
    def test_mc_update_new_name(self):
        # row with id 1(kg) is part of demo data
        request = self.request(media_contact.update)
        request['reqd'] = {'id': 1,
                          'contact': 'SomeNewUom'}

        response = self.send_request(request)
        response_status = response['resh']['status']

        self.assertEqual(response_status, 'OK')

    def test_mc_update_duplicate_name(self):
        # row with name 421-902-222-222 is part of demo data
        request = self.request(media_contact.update)
        request['reqd'] = {'id': 1,
                           'contact': '421-902-222-222'}

        response = self.send_request(request)

        self.assertStatus(response, 'OK')

    def test_mc_update_invalid_id(self):
        # rows with id over 100 are not part of demo data
        request = self.request(media_contact.update)
        request['reqd'] = {'id': 101,
                           'contact': 'm'}

        response = self.send_request(request,
                                     pass_interrupts=True)

        self.assertStatus(response, 'INT', 'business.record_no.is_invalid')

    def test_mc_update_missing_id(self):
        request = self.request(media_contact.update)
        request['reqd'] = {'contact': 'm'}

        response = self.send_request(request,
                                     pass_interrupts=True)

        self.assertStatus(response, 'INT', 'business.record_no.is_none')

    def test_mc_update_read(self):
        # 123-456-789 is not part of demo data
        mc_id_to_update = 1
        mc_name_to_update = '123-456-789'
        update_request = self.request(media_contact.update)
        update_request['reqd'] = {'id': mc_id_to_update,
                                  'contact': mc_name_to_update}

        self.send_request(update_request)
        select_request = self.request(media_contact.select)
        select_request['reqd'] = {'id': mc_id_to_update}

        select_response = self.send_request(select_request)
        response_status = select_response['resh']['status']
        select_resd = select_response['resd']
        mc_name_updated = select_resd[0]['contact']

        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(select_resd), 1)
        self.assertEqual(mc_name_updated, mc_name_to_update)
